package com.ruoyi.web.service.warning.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.web.domain.device.DeviceInfo;
import com.ruoyi.web.domain.warning.WarningFactorRecord;
import com.ruoyi.web.mapper.warning.WarningFactorRecordMapper;
import com.ruoyi.web.pojo.request.warning.WarningFactorRecordRequest;
import com.ruoyi.web.pojo.request.warning.WarningRecordCountByDayRequest;
import com.ruoyi.web.pojo.vo.warning.WarningRecordCountByDayVO;
import com.ruoyi.web.pojo.vo.warning.WarningRecordCountVO;
import com.ruoyi.web.service.device.DeviceInfoService;
import com.ruoyi.web.service.warning.WarningFactorRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 预警记录 服务实现类
 * @author fwx
 * @date 2024-01-30 15:08:07
 */
@Service
public class WarningFactorRecordServiceImpl extends ServiceImpl<WarningFactorRecordMapper, WarningFactorRecord> implements WarningFactorRecordService {

    @Autowired
    private WarningFactorRecordMapper warningFactorRecordMapper;

    @Autowired
    private DeviceInfoService deviceInfoService;

    /**
    * 新增预警记录
    */
    @Override
    public void insert(WarningFactorRecord WarningRecord){
        super.save(WarningRecord);
    }

    @Override
    public void insertList(List<WarningFactorRecord> records){

        if(CollectionUtils.isEmpty(records)){
            return;
        }

        super.saveBatch(records);
    }

    /**
    * 修改预警记录
    */
    @Override
    public void update(WarningFactorRecord WarningRecord){
        super.updateById(WarningRecord);
    }

    /**
    * 删除预警记录
    */
    @Override
    public void delete(String id){
        super.removeById(id);
    }

    /**
    *  分页查询预警记录
    */
    @Override
    public void queryPage(IPage<WarningFactorRecord> page){
        QueryWrapper<WarningFactorRecord> query  = new QueryWrapper<>();
        super.page(page,query);
    }

    @Override
    public List<WarningFactorRecord>  getLastRecord(WarningFactorRecordRequest request){
        return warningFactorRecordMapper.getLastRecord(request);
    }

    @Override
    public List<WarningFactorRecord>  queryPage(WarningFactorRecordRequest request){
        QueryWrapper<WarningFactorRecord> query  = new QueryWrapper<>();
        request.bulidWrapper(query);

        if(request.getUserId() != null){
            List<DeviceInfo> infos = deviceInfoService.getByUser();
            if(infos.size() == 0){
                return new ArrayList<>();
            }
            query.lambda().in(WarningFactorRecord::getDeviceId,infos.stream().map(DeviceInfo::getDeviceId).collect(Collectors.toList()));
        }

        PageUtils.startPage();
        List<WarningFactorRecord> list = super.list(query);
        return list;
    }

    @Override
    public List<WarningFactorRecord> getNewList(){
        return warningFactorRecordMapper.getNewList();
    }

    @Override
    public Long countByDay(String start, String end){
        QueryWrapper<WarningFactorRecord> query  = new QueryWrapper<>();
        query.lambda().between(WarningFactorRecord::getWarningTime,start,end);
        Long count = super.count(query);
        return count;
    }

    @Override
    public WarningRecordCountVO getFactorRecordCount(){

        WarningRecordCountVO result = new WarningRecordCountVO();

        Long all = super.count();
        Long month = this.countByDay(DateUtils.getMonthBeginDate(), DateUtils.getTomorrowStr());
        Long day = this.countByDay(DateUtils.getNowStr(), DateUtils.getTomorrowStr());

        result.setAllCount(all);
        result.setMonthCount(month);
        result.setDayCount(day);
        return result;
    }

    @Override
    public List<WarningRecordCountByDayVO> getFactorRecordCountByDay(WarningRecordCountByDayRequest request){

        List<WarningFactorRecord> list = this.getListByDay(request.getStartTime(), request.getEndTime());

        List<WarningRecordCountByDayVO> result = new ArrayList<>();

        if(CollectionUtils.isEmpty(list)){
            return result;
        }

        Map<String, List<WarningFactorRecord>> recMap
                = list.stream().collect(Collectors.groupingBy(rec -> DateUtils.formatDate(rec.getWarningTime())));

        for (String date : recMap.keySet()) {
            WarningRecordCountByDayVO vo = new WarningRecordCountByDayVO();
            vo.setDateStr(date);
            vo.setWarningNum(recMap.get(date).size());
            vo.setFactorRecords(recMap.get(date));
            result.add(vo);
        }

        return result;
    }

    @Override
    public List<WarningFactorRecord> getListByDay(String start, String end){
        QueryWrapper<WarningFactorRecord> query  = new QueryWrapper<>();
        query.lambda().between(WarningFactorRecord::getWarningTime,start,end);
        List<WarningFactorRecord> list = super.list(query);
        return list;
    }

    /**
    * 查询所有预警记录
    */
    @Override
    public List<WarningFactorRecord> queryAll(){
        QueryWrapper<WarningFactorRecord> query  = new QueryWrapper<>();
        List<WarningFactorRecord> list = super.list(query);
        return list;
    }

    /**
    * 根据id查询预警记录
    */
    @Override
    public WarningFactorRecord findById(String id){
        WarningFactorRecord res = super.getById(id);
        return res;
    }

}
